package com.siwei.mes.service.experiment.impl;

import com.siwei.mes.entity.experiment.ExperimentGenConfig;
import com.siwei.mes.enums.CheckEnum;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.enums.TestProjectEnum;
import com.siwei.mes.mapper.ExperimentCheckWaybillWeekMapper;
import com.siwei.mes.mapper.experiment.ExperimentCheckItemorderMapper;
import com.siwei.mes.mapper.experiment.ExperimentGenConfigMapper;
import com.siwei.mes.mapper.experiment.ExperimentYearConfigMapper;
import com.siwei.mes.service.experiment.ExperimentGenConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验台账生成委托规则设置(ExperimentGenConfig)表服务实现类
 *
 * @author linzi
 * @since 2024-05-22 21:06:06
 */
@Slf4j
@Service
public class ExperimentGenConfigServiceImpl implements ExperimentGenConfigService {
    @Resource
    private ExperimentGenConfigMapper experimentGenConfigMapper;
    @Resource
    private ExperimentCheckItemorderMapper experimentCheckItemorderMapper;
    @Resource
    private ExperimentYearConfigMapper experimentYearConfigMapper;
    @Resource
    private ExperimentCheckWaybillWeekMapper experimentCheckWaybillWeekMapper;

    @Override
    public void update(ExperimentGenConfig entity) {
        ExperimentGenConfig experimentGenConfig = experimentGenConfigMapper.selectByPrimaryKey(entity.getId());
        assertTrue(experimentGenConfig == null, "试验台账生成委托规则设置信息不存在");
        experimentGenConfigMapper.updateByPrimaryKeySelective(entity);

        /**这里清空混凝土相关的*/
        this.clearConcreteInfo(experimentGenConfig, entity);
        /**这里清空对应抗渗等级和抗氯离子*/
        //this.clearConcreteInfoByKsdjOrKllz(experimentGenConfig, entity);
        /**这里清空对应原材料按时间配置的*/
        this.clearCheckWeekInfo(experimentGenConfig, entity);
    }

    private void clearCheckWeekInfo(ExperimentGenConfig beforeInfo, ExperimentGenConfig newInfo) {
        Integer experimentType = beforeInfo.getExperimentType();
        /**如果等于混凝土类型 直接跳过*/
        if (newInfo.getExperimentType().equals(MaterialsEnum.CONCRETE.getCode())) {
            return;
        }

        Integer checkRulesBefore = beforeInfo.getCheckRules();
        Integer checkRulesNew = newInfo.getCheckRules();
        if (checkRulesBefore == null || checkRulesNew == null) {
            return;
        }
        /**如果两个检测类型不一致 需要清空之前表的数据*/
        if (!checkRulesBefore.equals(checkRulesNew)) {
            /**这里目前应该只需要处理混凝土抗压强度 批检的*/
            log.info("修改试验台账生成委托规则设置，开始清空【pk_experiment_check_waybill_week】表中，experimentType：【{}】，checkType：【{}】所有批检关联关系", experimentType, beforeInfo.getCheckType());
            experimentCheckWaybillWeekMapper.deleteByExperimentTypeAndCheckType(experimentType, beforeInfo.getCheckType());
        }
        /**如果这里是按时间 只是对应的周几修改了 也需要清空*/
        if (checkRulesBefore == 4 && checkRulesNew == 4) {
            String beforeInfoCheckThreshold = beforeInfo.getCheckThreshold();
            String newInfoCheckThreshold = newInfo.getCheckThreshold();
            if (!beforeInfoCheckThreshold.equals(newInfoCheckThreshold)) {
                /**这里目前应该只需要处理混凝土抗压强度 批检的*/
                log.info("修改对应周几生成试验台账生成委托规则设置，开始清空【pk_experiment_check_waybill_week】表中，experimentType：【{}】，checkType：【{}】所有批检关联关系", experimentType, beforeInfo.getCheckType());
                experimentCheckWaybillWeekMapper.deleteByExperimentTypeAndCheckType(experimentType, beforeInfo.getCheckType());
            }
        }
    }

    /**
     * 清空对应抗渗等级和抗氯离子
     *
     * @param beforeInfo 之前的数据
     * @param newInfo    最新的数据
     */
    private void clearConcreteInfoByKsdjOrKllz(ExperimentGenConfig beforeInfo, ExperimentGenConfig newInfo) {
        if (!newInfo.getExperimentType().equals(MaterialsEnum.CONCRETE.getCode())) {
            return;
        }

        String testCode = newInfo.getTestCode();
        if (StringUtils.isBlank(testCode)) {
            return;
        }
        List<String> testProjectList = new ArrayList<>();
        testProjectList.add(TestProjectEnum.CONCRETE_PARAM_KSDJ.getCode());
        testProjectList.add(TestProjectEnum.CONCRETE_PARAM_XNBG_LLZHL.getCode());
        if (!testProjectList.contains(testCode)) {
            return;
        }
        boolean isClear = false;
        if (!beforeInfo.getIsProjectWithMix().equals(newInfo.getIsProjectWithMix())) {
            isClear = true;
        }

        if (!beforeInfo.getIsYear().equals(newInfo.getIsYear())) {
            isClear = true;
        }

        if (isClear) {
            log.info("修改试验台账生成委托规则设置，开始清空【experimentYearConfig】表中，试验项目：【{}】所有关联关系", testCode);
            experimentYearConfigMapper.deleteByTestCode(testCode);
        }
    }

    /**
     * 清空混凝土相关配置
     *
     * @param beforeInfo 之前的数据
     * @param newInfo    最新的数据
     */
    private void clearConcreteInfo(ExperimentGenConfig beforeInfo, ExperimentGenConfig newInfo) {
        if (!newInfo.getExperimentType().equals(MaterialsEnum.CONCRETE.getCode())) {
            return;
        }

        String testCode = newInfo.getTestCode();
        if (StringUtils.isBlank(testCode)) {
            return;
        }
        List<String> testProjectList = new ArrayList<>();
        testProjectList.add(TestProjectEnum.CONCRETE_PARAM_KYQD.getCode());
        testProjectList.add(TestProjectEnum.CONCRETE_PARAM_KZQD.getCode());
        if (!testProjectList.contains(testCode)) {
            return;
        }

        Integer checkRulesBefore = beforeInfo.getCheckRules();
        Integer checkRulesNew = newInfo.getCheckRules();
        if (checkRulesBefore == null || checkRulesNew == null) {
            return;
        }
        /**如果两个检测类型不一致 需要清空之前表的数据*/
        if (!checkRulesBefore.equals(checkRulesNew)) {
            /**这里目前应该只需要处理混凝土抗压强度 批检的*/
            log.info("修改试验台账生成委托规则设置，开始清空【experimentCheckItmeorder】表中，试验项目：【{}】所有批检关联关系", testCode);
            experimentCheckItemorderMapper.deleteBatchInfo(testCode, CheckEnum.BATCH.getCode(), null);
        }

    }

    @Override
    public List<ExperimentGenConfig> getAll(ExperimentGenConfig entity) {
        return experimentGenConfigMapper.getList(entity);
    }
}
